﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Itegracion.Framework.Forms;
using System.Reflection;

namespace Itegracion.Browser
{
    public partial class Main : fmBase
    {
        private Dictionary<int, string> config;
        public Main()
        {
            InitializeComponent();
            config = new Dictionary<int, string>();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            fmLogin fm = new fmLogin();
            if (fm.ShowDialog() == DialogResult.OK)
            {
                tsslUser.Text = string.Format("{0} - {1} {2}", fm.User.Rows[0]["nombreUsuario"], fm.User.Rows[0]["nombres"], fm.User.Rows[0]["apellidos"]);
                TreeNode raiz = tvNavegador.Nodes.Add(string.Format("Empresa: {0}",fm.User.Rows[0]["Empresa"]));
                tsslRazonSocial.Text = fm.User.Rows[0]["razonSocial"] + "";
                tsslRUC.Text = fm.User.Rows[0]["ruc"] + "";

                DataTable dt = new DataTable();
                _("Itegracion.Framework","User", "TraerMenu", new Element("usuario", fm.User.Rows[0]["nombreUsuario"]))
                    .Out(dt);
                RenderNodes(raiz, dt);

                dt = new DataTable();
                _("Itegracion.Framework", "Constante", "TraerConfiguracion")
                    .Out(dt);
                FillConfig(dt);

                tsslTC.Text = string.Format("S/.{0}", config[1]);
                tsslIGV.Text = string.Format("{0}%", config[2]);
            }
            tvNavegador.ExpandAll();
        }

        private void FillConfig(DataTable dt)
        {
            config.Clear();
            foreach (DataRow item in dt.Rows)
            {
                config.Add(Convert.ToInt32(item["id"]), item["valor"] + "");
            }
        }

        private void RenderNodes(TreeNode raiz, DataTable dt)
        {
            TreeNode node;
            TreeNode parent;
            foreach (DataRow dr in dt.Rows)
            {
                parent = (string.IsNullOrEmpty(dr["idPadre"] + "") ? raiz : tvNavegador.Nodes.Find("node" + dr["idPadre"], true)[0]);
                node = new TreeNode(dr["nombre"] + "");
                node.Name = "node" + dr["id"];
                node.Tag = string.Format("{0}:{1}", dr["ensamblado"], dr["clase"]);
                parent.Nodes.Add(node);
            }
        }

        private void tvNavegador_DoubleClick(object sender, EventArgs e)
        {
            TreeNode node = tvNavegador.SelectedNode;
            if (node.Nodes.Count == 0)
            {
                string assembly = (node.Tag as string).Split(':')[0];
                string type = (node.Tag as string).Split(':')[1];
                
                Type tipo = Assembly.LoadFrom(string.Format("{0}.dll",assembly)).GetType(string.Format("{0}.Forms.{1}",assembly, type));

                DataTable dt = new DataTable();
                _("Itegracion.Framework", "Constante", "TraerConfiguracion")
                    .Out(dt);
                FillConfig(dt);

                fmBase fm = Activator.CreateInstance(tipo) as fmBase;
                fm.MdiParent = this;
                fm.Configuration = config;
                fm.Show();
            }
        }


    }
}
